//
//  MYWelcomeViewController.swift
//  MYSinaWeico
//
//  Created by Magic Yu on 16/4/11.
//  Copyright © 2016年 Magic Yu. All rights reserved.
//

import UIKit
import SDWebImage

class MYWelcomeViewController: UIViewController {

    override func loadView() {
        view = UIImageView(image: UIImage(named: "ad_background"))
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        setUpUI()
        //在viewDidLoad中获取的根视图大小可能不准确
        print(view.frame)
    
    }
    
    override func viewWillLayoutSubviews() {
        //会自动给根视图大小重新设置和设备的屏幕尺寸大小一致的size
        super.viewWillLayoutSubviews()
        print(view.frame)
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        print(view.frame)
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        print(view.frame)
        //执行动画
        startAnimation()
    }
    
    //设置控件
    private func setUpUI() {
    
        view.addSubview(iconView)
        view.addSubview(nameLabel)
        
        //将用户头像处理成圆角
        iconView.cornerRadius = 45
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view.snp_bottom).offset(-180)
            make.size.equalTo(CGSize(width: 90, height: 90))
            
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        
        //加载头像
        //网络提供的数据不能够直接使用 需要转换数据(洗数据)
        iconView.sd_setImageWithURL(MYUserAccountViewModel.sharedAccountViewModel.headImageURL)
    }
    
    private func startAnimation() {
    
        let offset = -screenHeight + 180
        //动画效果要修改的代码 frame布局 修改frame
        self.iconView.snp_updateConstraints { (make) -> Void in
            //更新头像的底部约束
            make.bottom.equalTo(self.view.snp_bottom).offset(offset)
        }
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            //强制刷新页面
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                print("ok")
        }
    }

    
    //懒加载控件
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    private lazy var nameLabel:UILabel = {
        
        let l = UILabel()
        l.textColor = UIColor.darkGrayColor()
        l.font = UIFont.systemFontOfSize(15)
        //设置文字
        l.text = MYUserAccountViewModel.sharedAccountViewModel.userAccount?.name
        l.sizeToFit()
        return l
        
    }()
    
}









